package nl.saxion.act.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.saxion.act.dao.AuctionDAO;
import nl.saxion.act.dao.CatagoryDAO;
import nl.saxion.act.dao.PersonDAO;
import nl.saxion.act.db.DatabaseManager;
import nl.saxion.act.wt.Auction;
import nl.saxion.act.wt.Person;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DatabaseManager dbMan;
	private ServletContext context;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init() throws ServletException {
    	context = this.getServletContext();
    	dbMan = (DatabaseManager) context.getAttribute("dbMan");   	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String adress = request.getParameter("adress");
		String city = request.getParameter("city");
		String email = request.getParameter("email");
		
		PersonDAO p = new PersonDAO(dbMan);
		Person user = p.insertPerson(name, adress, city, email, username, password);
		
		if (p != null) {
			HttpSession session = request.getSession(true);
			session.setAttribute("currentSessionUser", "yes");
			session.setAttribute("personID", user.getId() );
			response.sendRedirect("VeilingServlet"); //logged-in page
		}

	}

}
